package handler

import (
	"strconv"
	"web_framework/config"
	"web_framework/model"
	"web_framework/pkg/constvar"
	"web_framework/pkg/e"
	"web_framework/storage"
)

//登录身份验证
type AppLoginHandle struct {
}

func (appLoginHandle AppLoginHandle) handleMsg(data model.Request, clientId string) (result model.Response) {
	if data.Mtype == constvar.AppInitRequestMtype {
		//检查参数是否为空
		if data.Data.UID == 0 || data.Data.Token == "" || data.Data.Sver == 0 {
			e.Log("Info", "uid/token/sver参数异常")
			return ReturnErr(data.Mid, constvar.AppInitResponeMtype, "uid/token/sver参数异常")
		}
		//检查版本号
		sverInt := strconv.Itoa(data.Data.Sver)
		err := e.CheckVersion(sverInt)
		if err != nil {
			e.Log("Info", "检查版本失败", err.Error())
			return ReturnErr(data.Mid, constvar.DownloadInitResponeMtype, err.Error())
		}

		//查询初始化配置
		downConfig, err := storage.GetDownloadConfig(config.C.DB.DB, 1)
		if err != nil {
			e.Log("Info", "查询长连接初始化配置失败")
			return ReturnErr(data.Mid, constvar.AppInitResponeMtype, "查询长连接初始化配置失败")
		}
		var returnData interface{} = model.ResponseData{
			HbInterval: downConfig.HbInterval,
		}
		e.Log("Info",clientId, "登录成功")
		return model.Response{
			Mid:    data.Mid,
			Mtype:  constvar.AppInitResponeMtype,
			Info:   "登录成功",
			Status: constvar.StatusOk,
			Data:   &returnData,
		}
	}
	e.Log("Info", "APP登录失败")
	return ReturnErr(data.Mid, constvar.AppInitResponeMtype, "APP登录失败!")
}
